import axios from 'axios'

export const getJobList = (page_id, env_id) => {
  return axios.get('/jobs/list', { params: { page_id, env_id } })
}

export const getTaskList = (page_id, job_id) => {
  return axios.get('/jobs/tasks', { params: { page_id, job_id } })
}

export const runJob = (job_id) => {
  return axios.post('/jobs/run', { job_id })
}

export const abortJob = (job_id) => {
  return axios.post('/jobs/abort', { job_id })
}

export const submitJob = (env_id, parallel_count, parallel_config, aggregate_config) => {
  const formData = new FormData()
  formData.append('env_id', env_id)
  if (parallel_count) formData.append('parallel_count', parallel_count)
  if (parallel_config) formData.append('parallel_config', parallel_config)
  if (aggregate_config) formData.append('aggregate_config', aggregate_config)
  return axios.post('/jobs/submit', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const showTaskLog = (task_id) => {
  return axios.get('/jobs/task-log', { params: { task_id } })
}

export const downloadTaskLog = (task_id) => {
  return axios.get('/jobs/download-log', { params: { task_id }, responseType: 'blob' })
}
